//
//  HXMainTabBar.swift
//  HXSwift
//
//  Created by apple　　　 on 2016/11/22.
//  Copyright © 2016年 yangguang. All rights reserved.
//

import UIKit

class HXMainTabBar: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        self .addChildViewController(childControllerName: "HXHomeController", title: "首页", normalImage: "TabBar1")
//        self .addChildViewController(childControllerName: "HXBrawController", title: "提额", normalImage: "TabBar2")
//        self .addChildViewController(childControllerName: "HXMyController", title: "我的", normalImage: "TabBar3")
        
        
        setupChildControllers()
        
    }
    
    //MARK:设置子控制器
    private func addChildViewController(childControllerName : String,title : String,normalImage : String) {
        
        // 1.获取命名空间
        guard let namespace = Bundle.main.infoDictionary!["CFBundleName"] else {
            print("命名空间不存在")
            return
        }
        
        // 2.通过命名空间和类名转换成类
        let cls : AnyClass? = NSClassFromString((namespace as! String) + "." + childControllerName)
        
        // swift 中通过Class创建一个对象,必须告诉系统Class的类型
        guard let clsType = cls as? UIViewController.Type else {
            print("无法转换成UITableViewController")
            return
        }
        
        // 3.通过Class创建对象
        let childController = clsType.init()
        
        // 设置TabBar和Nav的标题
        childController.title = title
        childController.tabBarItem.image = UIImage(named: normalImage)
        childController.tabBarItem.selectedImage = UIImage(named: normalImage + "Sel")
        
        // 包装导航控制器
        let nav = UINavigationController(rootViewController: childController)
        self.addChildViewController(nav)
    }
    

    
    //MARK:设置所有控制器
    private func setupChildControllers() {
        
        //创建一个数组
        let array = [
            
            ["clsName": "HXHomeController", "title": "首页", "imageName": "TabBar1"],
            ["clsName": "HXBrawController", "title": "提额", "imageName": "TabBar2"],
            ["clsName": "HXMyController", "title": "我的", "imageName": "TabBar3"],
        ]
        
        //创建UIViewController数组
        var arrayM = [UIViewController]()
        
        //遍历字典追加到数组中
        for dict in array {
            
            arrayM.append(controller(dict: dict))
            
        }
        
        viewControllers = arrayM
    }
    
    
    //MARK:设置子控制器
    private func controller(dict:[String: String]) -> UIViewController {
        
        //守护常量是否有值
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                return UIViewController()
        }
        
        //创建视图控制器
        let vc = cls.init()
        vc.title = title
        
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named:imageName + "Sel")?.withRenderingMode(.alwaysOriginal)
        
        //创建NavigationController
        let nvc = HXNavigationController(rootViewController: vc)
        
        return nvc

    }
 
}

